<template>
  <div class="table">
    <div class="header">
      <slot name="header">
        <div class="title">{{ $attrs.tableTitle }}</div>
        <div class="handle">
          <slot name="handerHandle"></slot>
        </div>
      </slot>
    </div>
    <el-table
      :data="tableData"
      border
      stripe
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="80px"
        align="center"
        v-if="showSelectColumn"
      ></el-table-column>
      <el-table-column
        label="序号"
        type="index"
        width="80px"
        align="center"
        v-if="showIndexColumn"
      ></el-table-column>
      <template v-for="item in propList" :key="item.prop"
        ><el-table-column v-bind="item" align="center" show-overflow-tooltip>
          <template #default="{ row }">
            <slot :name="item.slotName" :row="row">
              {{ row[item.prop] }}
            </slot>
          </template>
        </el-table-column></template
      >
    </el-table>
    <div class="footer">
      <slot name="footer">
        <el-pagination
          :currentPage="pageInfo.crrentpage"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="pageInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageNumber"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </slot>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue'

export default defineComponent({
  props: {
    tableData: {
      type: Array,
      required: true
    },
    propList: {
      type: Array,
      required: true
    },
    showIndexColumn: {
      type: Boolean,
      default: false
    },
    showSelectColumn: {
      type: Boolean,
      default: false
    },
    pageInfo: {
      type: Object,
      default: () => ({ crrentpage: 0, pagesize: 10 })
    },
    pageNumber: {
      type: Number,
      default: 0
    }
  },
  emits: ['update:pageInfo'],
  setup(props, { emit }) {
    const handleSelectionChange = (value: any) => {
      console.log(value)
    }
    const handleCurrentChange = (currentpage: number) => {
      emit('update:pageInfo', { ...props.pageInfo, currentpage })
    }
    const handleSizeChange = (pagesize: number) => {
      emit('update:pageInfo', { ...props.pageInfo, pagesize })
    }
    return {
      handleSelectionChange,
      handleSizeChange,
      handleCurrentChange
    }
  }
})
</script>

<style scoped lang="less">
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 45px;
  padding: 0 5px;
  .title {
    font-size: 20px;
    font-weight: 700;
  }
}
.footer {
  margin-top: 14px;
  .el-pagination {
    text-align: center;
  }
}
</style>
